package com.fourmob.poppyview.librarys;

import ohos.aafwk.ability.Ability;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.render.Paint;
import ohos.agp.utils.LayoutAlignment;

public class PoppyViewHelper {
    public enum PoppyViewPosition {
        TOP, BOTTOM
    }

    private static final int SCROLL_TO_TOP = -1;

    private static final int SCROLL_TO_BOTTOM = 1;

    private static final int SCROLL_DIRECTION_CHANGE_THRESHOLD = 5;

    private Ability mActivity;

    private LayoutScatter mLayoutInflater;

    private Component mPoppyView;

    private int mScrollDirection = 0;

    private int moveFromY = 0;
    private int moveToY = 0;
    private int moveFromYs = 0;
    private int moveToYs = 0;
    private int num = 300;
    private PoppyViewPosition mPoppyViewPosition;
    private Paint mPaint = new Paint();

    {
        /**
         * 防抖动
         */
        mPaint.setDither(true);
        /**
         * 抗锯齿，降低分辨率，提高绘制效率
         */
        mPaint.setAntiAlias(true);
    }

    public PoppyViewHelper(Ability activity, PoppyViewPosition position) {
        mActivity = activity;
        mLayoutInflater = LayoutScatter.getInstance(activity);
        mPoppyViewPosition = position;
    }

    public PoppyViewHelper(Ability activity) {
        this(activity, PoppyViewPosition.BOTTOM);
    }

    // for scrollview
    public Component createPoppyViewOnScrollView(int scrollViewId, int poppyViewResId) {
        mPoppyView = mLayoutInflater.parse(poppyViewResId, null, true);
        final NotifyingScrollView scrollView = (NotifyingScrollView) mActivity.findComponentById(scrollViewId);
        initPoppyViewOnScrollView(scrollView);
        return mPoppyView;
    }


    // for ListView

    public Component createPoppyViewOnListView(int listViewId, int poppyViewResId, Component.ScrolledListener onScrollListener) {
        ListContainer listView = (ListContainer) mActivity.findComponentById(listViewId);
        mPoppyView = mLayoutInflater.parse(poppyViewResId, null, true);
        initPoppyViewOnListView(listView, onScrollListener);
        return mPoppyView;
    }

    public Component createPoppyViewOnListView(int listViewId, int poppyViewResId) {
        return createPoppyViewOnListView(listViewId, poppyViewResId, null);
    }


    // common

    private void setPoppyViewOnView(Component view) {
        ComponentContainer.LayoutConfig lp = view.getLayoutConfig();
        ComponentParent parent = view.getComponentParent();
        ComponentContainer group = (ComponentContainer) parent;
        int index = group.getChildIndex(view);
        final StackLayout newContainer = new StackLayout(mActivity);
        group.removeComponent(view);
        group.addComponent(newContainer, index, lp);
        newContainer.addComponent(view);
        final StackLayout.LayoutConfig layoutParams = new StackLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        layoutParams.alignment = mPoppyViewPosition == PoppyViewPosition.BOTTOM ? LayoutAlignment.BOTTOM : LayoutAlignment.TOP;
        newContainer.addComponent(mPoppyView, layoutParams);
        group.invalidate();
    }

    // for ScrollView
    private void initPoppyViewOnScrollView(NotifyingScrollView scrollView) {
        setPoppyViewOnView(scrollView);
        scrollView.setScrolledListener(new Component.ScrolledListener() {
            private int mScrollPositions = 0;

            @Override
            public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
                if (Math.abs(i1 - mScrollPositions) >= Component.SCROLL_AUTO_STAGE) {
                    int newScrollDirection = 0;
                    {
                        if (i1 < i3) {
                            newScrollDirection = SCROLL_TO_TOP;
                        } else {
                            newScrollDirection = SCROLL_TO_BOTTOM;
                        }
                    }
                    if (newScrollDirection != mScrollDirection) {
                        mScrollDirection = newScrollDirection;
                        mActivity.getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                AnimatorProperty animator = mPoppyView.createAnimatorProperty();
                                switch (mScrollDirection) {
                                    case -1:
//                                            moveFromY = 0;
//                                            moveToY = -mPoppyView.getHeight();
                                        break;
                                    case 1:
                                        if (moveToY < 0) {
                                            moveFromY = -mPoppyView.getHeight();
                                            moveToY = 0;
                                        } else {
                                            moveFromY = 0;
                                            moveToY = 0;
                                        }
                                        break;
                                }
                                if (moveToY == 0 && i1 < i3) {
                                    moveFromY = 0;
                                    moveToY = -mPoppyView.getHeight();
                                }
                                animator.moveFromY(moveFromY).moveToY(moveToY).setDuration(num);
                                animator.start();
                            }
                        });
                    }
                }
                mScrollPositions = i1;
            }
        });
    }

    // for ListView
    private void initPoppyViewOnListView(ListContainer listView, final Component.ScrolledListener onScrollListener) {
        setPoppyViewOnView(listView);

        listView.setScrolledListener(new Component.ScrolledListener() {

            private int mScrollPosition = 0;

            @Override
            public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
                if (onScrollListener != null) {
                    onScrollListener.onContentScrolled(component, i, i1, i2, i3);
                }
                if (Math.abs(i1 - mScrollPosition) != Component.SCROLL_AUTO_STAGE) {
                    int newScrollDirection;
                    if (i1 < i3) {
                        newScrollDirection = SCROLL_TO_TOP;

                    } else {
                        newScrollDirection = SCROLL_TO_BOTTOM;

                    }
                    if (newScrollDirection != mScrollDirection) {
                        mScrollDirection = newScrollDirection;
                        mActivity.getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {

                                AnimatorProperty animator = mPoppyView.createAnimatorProperty();
                                switch (mScrollDirection) {
                                    case -1:
                                        moveFromYs = listView.getBottom();
                                        moveToYs = listView.getBottom() - mPoppyView.getHeight();
                                        break;
                                    case 1:
                                        moveFromYs = listView.getBottom() - mPoppyView.getHeight();
                                        moveToYs = listView.getBottom();
                                        break;
                                }
                                animator.moveFromY(moveFromYs).moveToY(moveToYs).setDuration(num);
                                animator.start();
                            }
                        });
                    }
                }
                mScrollPosition = i1;
            }

        });
    }
}
